import com.ailk.kbi.kbirazor.control.AreaControl;
import com.ailk.kbi.kbirazor.model.AreaModel;
import com.ailk.kbi.kbirazor.model.KbrModel;
import com.ailk.kbi.kbirazor.vo.DimVO;
import com.ailk.kbi.kbirazor.vo.KpiVO;

import flash.events.Event;
import flash.events.MouseEvent;

import mx.charts.series.ColumnSeries;
import mx.charts.series.LineSeries;
import mx.collections.XMLListCollection;
import mx.controls.AdvancedDataGrid;
import mx.controls.Alert;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.core.IFlexDisplayObject;
import mx.events.DragEvent;
import mx.events.FlexEvent;
import mx.managers.DragManager;
import mx.managers.PopUpManager;



[Bindable]
private var areaModel:AreaModel = AreaModel.getInstance();
[Bindable]
private var kbrModel:KbrModel = KbrModel.getInstance();

private var areaControl:AreaControl = new AreaControl();


/**表格列的数据*/
private var columns:Array = new Array();

/**存储图表的序列数组*/
private var series:Array = new Array();

/**初始化窗口*/
protected function creationCompleteHandler(event:FlexEvent):void {
	var dgc:AdvancedDataGridColumn = new AdvancedDataGridColumn();
	dgc.headerText = "地域";
	dgc.width = 130;
	dgc.minWidth = 130;
	dgc.dataField = "@cityName";
	columns.push(dgc);
	areaGrid.columns = columns;
	areaChart.series = series;
	
	btnMin.visible = false; 
}
/**关闭窗口*/
private function closeHandler(event:Event):void {
	event.target.removeEventListener("close",closeHandler);
	PopUpManager.removePopUp(event.target as IFlexDisplayObject);
	areaModel.windowFlag = false;
	areaModel.dataXML = <result></result>;
	areaGrid.columns = null;
	
	columns = new Array();
	series = new Array();
	areaModel.kpiVOList.removeAll();
	areaModel.kpiIdList.removeAll();
	
}

private function dragEnterHandler(event:DragEvent):void {
	if (event.dragSource.hasFormat('KpiVO')) { 
		var dropTarget:AdvancedDataGrid=AdvancedDataGrid(event.currentTarget); 
		DragManager.acceptDragDrop(dropTarget); 
	} 
}
private function dragDropHandler(event:DragEvent):void {
	var kpiVO:KpiVO = event.dragSource.dataForFormat('KpiVO') as KpiVO;
	
	for each(var kpiVONow:KpiVO in areaModel.kpiVOList) {
		if(kpiVO.kpiId == kpiVONow.kpiId) {
			Alert.show("该项指标已经存在！");
			return;
		}
	}
	
	areaModel.kpiVOList.addItem(kpiVO);
	areaModel.kpiIdList.addItem(kpiVO.kpiId);
	
	areaModel.dataXML.data.@cityName = kpiVO.cityName;
	areaModel.dataXML.data.@["kpiValue_"+kpiVO.kpiId] = kpiVO.kpiValue;
	areaModel.dataXML.data.@["kpiId_"+kpiVO.kpiId] = kpiVO.kpiId;
	areaModel.dataXML.data.@cityId = kpiVO.cityId;
	areaModel.dataXML.data.@areaId = kpiVO.areaId;
	areaModel.dataXML.data.@userId = kpiVO.userId;
	areaModel.dataXML.data.@opTime = kpiVO.opTime;
	
	
	if(areaModel.dataXML.data.children().length() > 0) {
		var xmlCollection:XMLListCollection = new XMLListCollection(areaModel.dataXML.data.children());
		xmlCollection.removeAll();
	}
	
	/*更新表格*/
	var dgc:AdvancedDataGridColumn = new AdvancedDataGridColumn();
	with(dgc){
		headerText = kpiVO.kpiName;
		dataField = "@kpiValue_" + kpiVO.kpiId;
		width = 100;
		minWidth = 100;
	}
	columns.push(dgc);
	areaGrid.columns = columns;
	
	
	/*更新图表*/
	var newSeries:ColumnSeries = new ColumnSeries();
	with(newSeries){
		displayName = dgc.headerText;
		yField = dgc.dataField;
	}
	series.push(newSeries);	
	areaChart.series = series;
}

/**下钻*/
private function viewDetail():void {
	//		Alert.show("kpi个数：" + kbrModel.areaWindowTitleArr.length);
	if(areaGrid.selectedIndex > -1){
		var selectedXML:XML = areaModel.selectedXML = areaGrid.selectedItem as XML;
		if(selectedXML.children().length()==0){
			//			Alert.show(areaGrid.selectedItem.@cityId);
			
			var dimVO:DimVO = new DimVO();
			
			dimVO.areaId = selectedXML.@areaId;
			dimVO.cityId = selectedXML.@cityId;
			dimVO.opTime = selectedXML.@opTime;
			
			areaModel.dimVO = dimVO;		
			
			if(dimVO.cityId=='999'){//全市
				areaControl.cityList();
			} else if(dimVO.cityId=='0000'){
				
			} else {
				areaControl.areaList(); 
			}
			//			else if(dimVO.areaId=='999'){//分公司
			//				overviewKpiControl.countylist();
			//			}
			//				//dimVO.kpiId && dimVO.cityId && dimVO.countyId && 
			
			//			dimVO.areaId &&
			//			else if(dimVO.schoolId=="9999"){
			//				overviewKpiControl.schoollist();
			//			}
		}		
	}
}

//导出数据
protected function image1_clickHandler(event:MouseEvent):void
{
	if(areaModel.kpiVOList.length >= 1) {
		var xml:XML = <excel/>;
		xml.@title = "地域分析" + kbrModel.currentDate + "日数据";
		var headerXML:XML = <header><column title="地域" name="@cityName" width="1" height="1"/></header>;
		
		for each(var kpiVO:KpiVO in areaModel.kpiVOList) {
			var headerColumnXML:XML = <column></column>;
			headerColumnXML.@title = kpiVO.kpiName;
			headerColumnXML.@name = "@kpiValue_"+kpiVO.kpiId;
			headerColumnXML.@width = "1";
			headerColumnXML.@height = "1";
			
			headerXML.appendChild(headerColumnXML);
		}
		
		headerXML.@height = 1;
		xml.appendChild(headerXML);
		
//		Alert.show(xml.toString());
		xml.appendChild(areaModel.dataXML);
		areaControl.createExcel(xml.toXMLString());
	} else {
		Alert.show("请先选择指标","提示");
	}
	
}